﻿Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Seuss.EnterpriseLibrary.DataAccess
Imports Seuss.EnterpriseLibrary.BusinessLogic
Imports System.Web
Imports System.Data

Partial Public Class Consulta_AL_CursoActual_Notas
    Inherits System.Web.UI.Page
    Dim nCurGruCodigo As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString("nCurGruCodigo") Is Nothing Or Request.QueryString("nCurGruCodigo") Is String.Empty Then
            If Session("nCurGruCodigo") Is Nothing Or Session("nCurGruCodigo") Is String.Empty Then
                Response.Redirect("AL_InfoAcad_CA.aspx")
            Else
                nCurGruCodigo = Session("nCurGruCodigo")
            End If
        Else
            Session("nCurGruCodigo") = Request.QueryString("nCurGruCodigo")
            nCurGruCodigo = Request.QueryString("nCurGruCodigo")
        End If
        Response.Expires = -1
        Response.AddHeader("Pragma", "no-cache")
        Response.AddHeader("cache-control", "private")
        Response.CacheControl = "no-cache"

        lnkRegresar_3.HRef = "./AL_CursoActual.aspx?nCurGruCodigo=" + nCurGruCodigo
        lnkRegresar_4.HRef = "./AL_CursoActual.aspx?nCurGruCodigo=" + nCurGruCodigo

        If Not Page.IsPostBack Then
            LlenaNotas()
            Using Seguridad As New BLL_Administracion
                Seguridad.Set_Transaccion_Insert(Nothing, 100401, IdentityUser.UserCode, My.Request.UserHostAddress.ToString, CrearXMLTransaccion("Consulta Pagina Movil", "/Pagina|" + My.Request.Url.Segments(My.Request.Url.Segments.Length - 1).ToString))
            End Using
        End If

    End Sub

    Private Sub LlenaNotas()
        Dim CodigoRegistro As String = String.Empty
        Dim CodigoMatricula As String = String.Empty
        Dim CodigoRegistroSec As String = String.Empty ' registro de la sección del alumno 
        Dim Promedio As Integer
        Dim SiProm As Boolean
        Dim lrd As New List(Of IDataReader)
        Using ObjPool As New PoolDbCommand()
            Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjMenu.Get_Registro_By_nCurGruCodigo(CapturaCurGrupoBase(Session("ncurgrucodigo"))), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.AddDbCommand(ObjMenu.Get_cDocCodigoMatricula_By_cPerCodigo_nCurGruCodigo(IdentityUser.UserCode, (Session("ncurgrucodigo"))), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.AddDbCommand(ObjMenu.Get_Registro_By_nCurGruCodigo(Session("ncurgrucodigo")), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, lrd, True)
            If lrd(0).Read Then
                CodigoRegistro = lrd(0).GetString(0)
            End If
            If lrd(1).Read Then
                CodigoMatricula = lrd(1).GetString(0)
            End If
            If lrd(2).Read Then
                CodigoRegistroSec = lrd(2).GetString(0)
            End If
        End Using
        lrd(0).Close()
        lrd(0) = Nothing
        lrd(1).Close()
        lrd(1) = Nothing
        lrd(2).Close()
        lrd(2) = Nothing
        Dim BodyNotas As String

        '******** PARCIALES ********
        Dim LdrPar As New List(Of IDataReader)
        Using ObjPool As New PoolDbCommand()
            Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjMenu.Get_Parciales_By_cDocCodigo(CodigoRegistro), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, LdrPar, True)
        End Using

        BodyNotas = "<table width='100%'>"
        Do While LdrPar(0).Read()
            Dim Notas As Boolean
            SiProm = False
            BodyNotas = BodyNotas + "<tr style='color:#FFFFFF'>"
            BodyNotas = BodyNotas + "<td colspan='2' style='background-color:#666666;font-size:12px;'><b>" + CStr(LdrPar(0).Item("cConDescripcion")) + "</b></td>"
            BodyNotas = BodyNotas + "</tr>"

            Dim LdrNotas As New List(Of IDataReader)
            Using ObjPool As New PoolDbCommand()
                Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
                ObjPool.AddDbCommand(ObjMenu.Get_Notas_By_Registro_Matricula_Descripcion(CodigoRegistroSec, CodigoMatricula, CStr(LdrPar(0).Item("nSubPrdCodigo")), CodigoRegistro), PoolDbCommand.PoolDbCommandResultType.Query)
                ObjPool.Pooling(Nothing, LdrNotas, True)
                If LdrNotas(0).RecordsAffected <> 0 Then
                    Notas = True
                End If
                If Notas = True Then
                    BodyNotas = BodyNotas + "<tr><td colspan=2><table style='background-color:#EDF8FC;' width='100%'>"
                End If
                Do While LdrNotas(0).Read()
                    BodyNotas = BodyNotas + "<tr><td width='70%' style='font-size:10px'>" + CStr(LdrNotas(0).GetString(2)) + " (" + CStr(LdrNotas(0).GetString(0)) + ")</td><td style='text-align:center; font-size:12px;'>" + CStr(LdrNotas(0).GetValue(1)) + "</td></tr>"
                    SiProm = True
                Loop
                If Notas = True Then
                    If SiProm = True Then
                        BodyNotas = BodyNotas + "<tr bgcolor=#BAE3F3><td style='font-size:12px'><b>PROMEDIO</b></td>"
                        Promedio = CDec(MostrarPromedio(CodigoMatricula, Session("ncurgrucodigo"), LdrPar(0).Item("nSubPrdCodigo")))
                        If Promedio <= 10 Then
                            BodyNotas = BodyNotas + "<td style='color:Red;text-align:center;font-size:12px'><b>" + CStr(MostrarPromedio(CodigoMatricula, Session("ncurgrucodigo"), LdrPar(0).Item("nSubPrdCodigo"))) + "<b></td>"
                        Else
                            BodyNotas = BodyNotas + "<td style='color:Blue;text-align: center;font-size:12px'><b>" + CStr(MostrarPromedio(CodigoMatricula, Session("ncurgrucodigo"), LdrPar(0).Item("nSubPrdCodigo"))) + "<b></td>"
                        End If
                        BodyNotas = BodyNotas + "</tr><tr bgcolor=#BAE3F3  style='font-size:12px'><td><b>FORMULA</b></td><td style='text-align:center; font-size:10px'>" + CStr(LdrPar(0).Item("cRegSubPrdFormula")) + "</td></tr>"
                    End If
                    BodyNotas = BodyNotas + "</tr></td></table><br/>"
                End If
            End Using
            LdrNotas(0).Close()
            LdrNotas(0) = Nothing
        Loop
        BodyNotas = BodyNotas + "</Table>"
        lblcontenido.Text = BodyNotas
        LdrPar(0).Close()
        LdrPar(0) = Nothing
    End Sub


    Public Function CapturaCurGrupoBase(ByVal nCurGruCodigo As Long) As Long
        Dim VCurGrupoBase As Long = 0
        Dim ldr As New List(Of IDataReader)
        Using ObjPool As New PoolDbCommand()
            Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjMenu.Get_nCurGruCodigoBase_By_nCurgruCodigo(nCurGruCodigo), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, ldr, True)
            If ldr(0).Read Then
                VCurGrupoBase = ldr(0).GetInt32(0)
            Else
                VCurGrupoBase = nCurGruCodigo
            End If
        End Using
        ldr(0).Close()
        ldr(0) = Nothing
        Return VCurGrupoBase
    End Function


    Private Function MostrarPromedio(ByVal cdoccodigo As String, ByVal ncurgrucodigo As Long, ByVal nsubprdcodigo As Integer) As Double
        Dim PromedioParcial As Double = 0.0
        Dim LdrNotaP As New List(Of IDataReader)
        Using ObjPool As New PoolDbCommand()
            Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjMenu.Get_PromedioParcial_By_cDocCodigo_nCurGruCodigo_nSubPrdCodigo(cdoccodigo, ncurgrucodigo, nsubprdcodigo), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, LdrNotaP, True)
            If LdrNotaP(0).Read Then
                PromedioParcial = LdrNotaP(0).GetDouble(0)
            Else
                PromedioParcial = 0.0
            End If
            Return PromedioParcial
        End Using
    End Function

End Class